package site.zhouinfo.servlet03;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import site.zhouinfo.utils.IdentifyCodeUtil;

/**
 * 读取表头信息 写入响应头信息
 *
 * @author zhou
 * @create 2017-06-27 10:38
 * @email zhouinfo@qq.com
 * @blog blog.zhouinfo.site
 */
public class ServletReadHeader extends HttpServlet {

    /**
     * 读取请求表头信息
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //Servlet中读取并显示当前的所有的请求头信息
        //请求头names集合
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            // 请求头name
            String headerName = headerNames.nextElement();
            // 请求头name对应的信息
            String header = req.getHeader(headerName);
            System.out.println(headerName+"\t: "+header);
        }
        doPost(req,resp);
    }

    /**
     * 写入响应表头信息
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置响应头信息 ContentType对应的数据类型 MIME类型
        resp.setContentType("image/jpeg");
        OutputStream outputStream = resp.getOutputStream();
        // 写入内容
        IdentifyCodeUtil.writeOutputStream(outputStream);
    }
}
